function upload(id, successFun, progressFun, rename) {
    var uploader = new plupload.Uploader({
        runtimes : 'html5,flash,silverlight,html4',
        browse_button : id,
        flash_swf_url : '../../plupload-2.1.2/js/Moxie.swf',
        silverlight_xap_url : '../../plupload-2.1.2/js/Moxie.xap',
        url : 'http://oss.aliyuncs.com',
        init: {
            //当init事件发生后触发监听函数
            PostInit: function() {
                setUploadParam(uploader);
            },

            //当文件添加到上传队列后触发监听函数
            /*FilesAdded: function(up, files) {
                plupload.each(files, function(file) {
                    document.getElementById('ossfile').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ')<b></b>'
                        +'<div class="progress"><div class="progress-bar" style="width: 0%"></div></div>'
                        +'</div>';
                });
            },*/

            //当队列中的某一个文件正要开始上传前触发监听函数
            BeforeUpload: function(up, file) {
                randomFileName(up, file, rename);

                if(progressFun){
                    $($('#'+id).find('.layui-progress')).show();
                }
            },

            //会在文件上传过程中不断触发，可以用此事件来显示上传进度监听函数
            UploadProgress: function(up, file) {
                if(progressFun){
                    progressFun(file.percent);
                }
            },

            //当文件添加到上传队列后触发监听函数
            FilesAdded: function(up, file){
                up.start();
            },

            //当队列中的某一个文件上传完成后触发监听函数
            FileUploaded: function(up, file, info) {
                if(successFun){
                    successFun(up, file, info);
                }
                if(progressFun){
                    $($('#'+id).find('.layui-progress')).hide();
                }
            },

            //当发生错误时触发监听函数
            Error: function(up, err) {
                console.log();
            }
        }
    });

    uploader.init();
}

//设置上传参数
function setUploadParam(uploader) {
    var nonce = Math.random().toString(36).slice(-4);
    var currTime = new Date().getTime();
    $.ajax({
        url: window.CONFIG.apiUrl + "base/getUploadSign",
        data: {},
        async: true,
        dataType: 'json',
        beforeSend: function(xhr) {
            xhr.setRequestHeader('nonce', nonce);
            xhr.setRequestHeader('currTime', currTime);
            xhr.setRequestHeader('checkSum', sha1(window.CONFIG.apiKey+nonce+currTime));
        },
        success: function (result) {
            result = JSON.parse(decrypt(result, window.CONFIG.aesKey));
            if(result.code == 0){
                var sign = result.data.sign;
                var multipart_params = {
                    key : sign.dir,
                    policy: sign.policy,
                    'OSSAccessKeyId': sign.accessKeyId,
                    'success_action_status' : '200', //让服务端返回200,不然，默认会返回204
                    'signature': sign.signature
                };
                uploader.setOption({
                    url: sign.host,
                    multipart_params: multipart_params
                });
            }else{
                layer.msg(result.msg);
            }
        }
    })


    /*post(signDomain, {}, false, function (data) {
        var sign = data.sign;
        var multipart_params = {
            key : sign.dir,
            policy: sign.policy,
            'OSSAccessKeyId': sign.accessKeyId,
            'success_action_status' : '200', //让服务端返回200,不然，默认会返回204
            'signature': sign.signature,
        };
        uploader.setOption({
            url: sign.host,
            multipart_params: multipart_params
        });
    })*/
}

//生成随机文件名
function randomFileName(uploader, file, rename) {
    var fileName = file.name;
    var multipart_params = uploader.getOption().multipart_params;
    var key = multipart_params.key;

    if(rename == undefined || rename){
        var suffix = fileName.substring(fileName.lastIndexOf('.'));
        key = key + getUUID() + suffix;
    }else{
        key = key + fileName;
    }

    multipart_params.key = key;
}